/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.click.app.webapp.action;

import com.click.app.DateFormatChanger;
import com.click.app.model.Purchase;
import com.click.app.model.Supplier;
import com.click.app.service.GenericManager;
import com.click.app.service.PurchaseManager;
import com.click.app.webapp.producers.PageViewCountData;
import com.click.app.webapp.producers.PurchaseMountReport;
import com.click.app.webapp.producers.PurchaseTotalMountReport;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 *
 * @author Joel Chuca Condo
 */
public class PurchaseReportAction extends BaseAction {

    private PurchaseManager purchaseManager;
    private GenericManager<Supplier, Long> supplierManager;
    private List<Supplier> suppliers;
    private Supplier supplier;
    private String dateStart;
    private String dateEnd;


    public void setPurchaseManager(PurchaseManager purchaseManager) {
        this.purchaseManager = purchaseManager;
    }

    public void setSupplierManager(GenericManager<Supplier, Long> supplierManager) {
        this.supplierManager = supplierManager;
    }

    public List getSuppliers() {
        return suppliers;
    }

    public void setSuppliers(List suppliers) {
        this.suppliers = suppliers;
    }

    public Supplier getSupplier() {
        return supplier;
    }

    public void setSupplier(Supplier supplier) {
        this.supplier = supplier;
    }

    public String getDateStart() {
        return dateStart;
    }

    public void setDateStart(String dateStart) {
        this.dateStart = dateStart;
    }

    public String getDateEnd() {
        return dateEnd;
    }

    public void setDateEnd(String dateEnd) {
        this.dateEnd = dateEnd;
    }   
    
    
    public String list(){
        //PageViewCountData page = new PageViewCountData();
        //getRequest().setAttribute("page", page);
        PurchaseTotalMountReport purchaseTMR;
        List<Purchase> purchases = new ArrayList();
        suppliers = supplierManager.getAll();
        for (Supplier supplierAux : suppliers) {
            Purchase purchaseAux = purchaseManager.findTotalMountPurchase(supplierAux); 
            purchases.add(purchaseAux);
        }

        if( purchases.size()== suppliers.size() ){
            purchaseTMR = new PurchaseTotalMountReport();
            purchaseTMR.setCategories(suppliers);
            purchaseTMR.setDatos(purchases);
            getRequest().setAttribute("purchaseTMR", purchaseTMR);
        }else
            return "error";
        return SUCCESS;
    }

    public String prepareSupplierPurchaseReport(){
        DateFormatChanger formater = new DateFormatChanger();
        dateStart = (String)getSession().getAttribute("dateStart");
        dateEnd = (String)getSession().getAttribute("dateEnd");
        if ( dateStart == null && dateEnd == null){
            dateStart = "01/01/2009";
            dateEnd = formater.changeDatetoString(new Date());
        }else{

        }

        PurchaseMountReport report = new PurchaseMountReport();
        
        Date date1 = formater.changeStringtoDate(dateStart);
        Date date2 = formater.changeStringtoDate(dateEnd); // today
        suppliers = supplierManager.getAll();
        Float[][] datas = (Float[][])purchaseManager.suppliersReport(date1, date2);
        
        report.setSupplierNames(suppliers);
        report.setDatas(datas);
        System.out.println(date1.getMonth());
        System.out.println(date2.getMonth());
        report.configMonths((date1.getMonth()), (date2.getMonth()));
        getRequest().setAttribute("report", report);
        return SUCCESS;
    }

    public Float[][] generar(){
        Float[][] f = new Float[5][5];
        for (int i = 0; i < f.length; i++) {
            for (int j = 0; j < f[0].length; j++) {
                f[i][j] = new Float(Math.random()*1000);
            }
        }
        return f;
    }


    public String generateSupplierPurchaseReport(){
        //DateFormatChanger formater = new DateFormatChanger();
        //Date date1 = formater.changeStringtoDate(dateStart);
        //Date date2 = formater.changeStringtoDate(dateEnd);

        getSession().setAttribute("dateStart", dateStart);
        getSession().setAttribute("dateEnd", dateEnd);
        return SUCCESS;
    }



   
}
